import { provide, inject, ref, unref } from 'vue'
import { message as Message } from 'ant-design-vue'
import { debounce } from 'lodash-es'
import { bindRolesList } from '@/api/role.js'
/**
 * Symbol
 */
export const columnsSymbol = Symbol('columns')
export const dataSourceSymbol = Symbol('dataSource')
export const querySymbol = Symbol('query')
export const lineheightSymbol = Symbol('lineheight')
export const loadingSymbol = Symbol('loading')
export const filtersUserSymbol = Symbol('filtersUser')
export const sortUserSymbol = Symbol('sortUser')

/**
 * 注册
 */
export function useProvideContextRegistry() {
  let columns = ref([])
  let dataSource = ref([])
  let query = ref({})
  let lineheight = ref()
  let loading = ref(false)
  let filtersUser = ref()
  let sortUser = ref()

  provide(columnsSymbol, columns)
  provide(dataSourceSymbol, dataSource)
  provide(querySymbol, query)
  provide(lineheightSymbol, lineheight)
  provide(loadingSymbol, loading)
  provide(filtersUserSymbol, filtersUser)
  provide(sortUserSymbol, sortUser)
}

/**
 * 获取值
 */
export function useColumns() {
  return inject(columnsSymbol)
}
export function useDataSource() {
  return inject(dataSourceSymbol)
}
export function useQuery() {
  return inject(querySymbol)
}
export function useLineheight() {
  return inject(lineheightSymbol)
}
export function useLoading() {
  return inject(loadingSymbol)
}
export function useFiltersUser() {
  return inject(filtersUserSymbol)
}
export function useSortUser() {
  return inject(sortUserSymbol)
}

/**
 * 管理工具
 */
export function useDataSourceManager() {
  let dataSource = useDataSource()
  let query = useQuery()
  let loading = useLoading()
  let filtersUser = useFiltersUser()
  let sortUser = useSortUser()
  let sortCreatetimeType = ref()
  let sortJoinTimeType = ref()
  let sortInternEndDateType = ref()
  let filtersUerStatustType = ref()
  let filtersIsPublishedType = ref()

  let update = debounce(async () => {
    loading.value = true
    let { current, pageSize, ...otherQuery } = query.value
    if (filtersUser.value) {
      filtersUerStatustType.value =
        filtersUser.value?.userStatus !== null
          ? filtersUser.value?.userStatus[0]
          : ''
      filtersIsPublishedType.value =
        filtersUser.value?.isActivated !== null
          ? filtersUser.value?.isActivated[0]
          : ''
    }
    if (sortUser.value) {
      if (sortUser.value.columnKey === 'gmtCreate') {
        sortInternEndDateType.value = ''
        sortJoinTimeType.value = ''
        sortCreatetimeType.value = sortUser.value.order
      } else if (sortUser.value.columnKey === 'internshipEndDate') {
        sortJoinTimeType.value = ''
        sortCreatetimeType.value = ''
        sortInternEndDateType.value = sortUser.value.order
      } else if (sortUser.value.columnKey === 'joinTime') {
        sortCreatetimeType.value = ''
        sortInternEndDateType.value = ''
        sortJoinTimeType.value = sortUser.value.order
      }
    }
    try {
      let result =
        (await bindRolesList({
          pageIndex: current,
          pageSize,
          keyWord: otherQuery.keyWord,
          gmtCreate: (sortCreatetimeType.value || '').replace(/end$/, ''),
          joinTime: (sortJoinTimeType.value || '').replace(/end$/, ''),
          internEndDate: (sortInternEndDateType.value || '').replace(
            /end$/,
            '',
          ),
          userStatus: filtersUerStatustType.value || '',
          isActivated: filtersIsPublishedType.value || '',
        })) || {}
      query.value.current = result?.data?.current || current
      query.value.pageSize = result?.data?.pageSize || pageSize
      query.value.total = result?.data?.totalCount || 0
      dataSource.value = result?.data?.userList
      // dataSource.value = ds()
      loading.value = false
    } catch (e) {
      Message.error('列表加载失败')
      loading.value = false
    }
  }, 300)

  return { update }
}
export function useQueryManager() {
  let query = useQuery()
  return {
    set(newQuery) {
      query.value = unref(newQuery)
    },
    assign(newQuery) {
      query.value = Object.assign({}, query.value, newQuery)
    },
  }
}
/**
 * 数据
 */
let ds = () =>
  Array(100)
    .fill(1)
    .map((i, index) => ({
      userId: index,
      key: index,
      userName: '23name' + index,
      workNumber: '342451' + index,
      mobile: '32314',
      userDept: 'sfshtr',
      userTitle: 'aassd',
      joinTime: '2024-11-11',
      gmtCreate: '2024-11-11 14:00:23',
      isActivated: 0,
      internshipEndDate: '2024-11-11',
      userStatus: 'job',
      userPwd: 'dald1',
      userAccount: '456789',
    }))
